package com.css.ztb.service.impl;

import com.css.ztb.domain.SysMenu;
import com.css.ztb.domain.SysUser;
import com.css.ztb.mapper.SysMenuMapper;
import com.css.ztb.service.MenuService;
import com.css.ztb.util.StringUtils;
import com.css.ztb.util.TreeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author shipeilei
 * @date 2020-7-1 10:11:17
 * @desc desc
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public Set<String> selectPermsByUserId(Long userId) {
        Set<String> menuSet = new HashSet<>();
        String perms = sysMenuMapper.selectPermsByUserId(userId);
        if (StringUtils.isNotNull(perms)) {
            menuSet.addAll(Arrays.asList(perms.trim().split(",")));
        }
        return menuSet;
    }

    @Override
    public List<SysMenu> selectMenusByUser(SysUser user) {
        List<SysMenu> menuList = new LinkedList<>();
        if (user.isAdmin()) {
            menuList = sysMenuMapper.selectMenuNormalAll();
        } else {
            menuList = sysMenuMapper.selectMenusByUserId(user.getUserId());
        }
        return TreeUtils.getChildPerms(menuList, 0);
    }
}
